package com.bbcare.nutritious.plat.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateTimeUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.JsonToEnetityUtil;
import com.bbcare.comm.ResultModel;
import com.bbcare.nutritious.plat.entity.PtGymnasticBarbell;
import com.bbcare.nutritious.plat.entity.PtWeight;
import com.bbcare.nutritious.plat.service.IGymnasticBarbellService;

/**
 * 体操杠铃和上下班控制器
 */
@Controller
@RequestMapping("/webapi/service/gymnasticBarbell")
public class GymnasticBarbellController {
	
    public static final Logger logger = Logger.getLogger(GymnasticBarbellController.class);
    
    @Autowired
    private IGymnasticBarbellService gymnasticBarbellService;

    /**
     * 保存或修改
     * @param request
     * @param jsonStr
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/gymnasticBarbellJoin")
    public ResultModel<Object> gymnasticBarbellJoin(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        logger.info("in gymnasticBarbellJoin----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        //获取实体参数
        PtGymnasticBarbell gymnasticBarbell = (PtGymnasticBarbell) JsonToEnetityUtil.jsonToObject(requestParam, PtGymnasticBarbell.class);
        //判空
        if(StringUtils.isEmpty(gymnasticBarbell.getUserId())){
        	logger.debug("out UserId----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"患者ID不能为空");
        }
        Map<String,Object> rspMap = gymnasticBarbellService.saveGymnasticBarbell(gymnasticBarbell);
        logger.debug("out weightJoin----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
    /**
     * 通过id或者userid+testDate查询
     * @return 单个对象
     */
    @ResponseBody
    @RequestMapping("/getgymnasticBarbellForUpdate")
    public ResultModel<Object> getgymnasticBarbellForUpdate(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        logger.info("in getgymnasticBarbellForUpdate----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        //获取实体参数
        PtGymnasticBarbell gymnasticBarbell = (PtGymnasticBarbell) JsonToEnetityUtil.jsonToObject(requestParam, PtGymnasticBarbell.class);
        //判空 id为空 并且userid或testDate为空
        if(StringUtils.isEmpty(gymnasticBarbell.getId()) && (StringUtils.isEmpty(gymnasticBarbell.getUserId()) || StringUtils.isEmpty(gymnasticBarbell.getTestDate()))){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参错误");
        }
        gymnasticBarbell.setStartDate("");
        gymnasticBarbell.setEndDate("");
        //查询体重列表
        Map<String,Object> rspMap = gymnasticBarbellService.listWeightByIdAndDate(gymnasticBarbell);
        logger.debug("out getgymnasticBarbellForUpdate----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
    /**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/listGymnasticBarbellForPage")
    public ResultModel<Object> listGymnasticBarbellForPage(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        logger.info("in listGymnasticBarbellForPage----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        //获取实体参数
        PtGymnasticBarbell gymnasticBarbell = (PtGymnasticBarbell) JsonToEnetityUtil.jsonToObject(requestParam, PtGymnasticBarbell.class);
        //判空
        if(StringUtils.isEmpty(gymnasticBarbell.getUserId())){
        	logger.debug("out UserId----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"患者ID不能为空");
        }
        if(gymnasticBarbell.getCurPage()<1){
        	logger.debug("分页页码小于1");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"分页页码不能小于1");
        }
        //日期查询条件判断
        if(!StringUtils.isEmpty(gymnasticBarbell.getStartDate())){
        	if(StringUtils.isEmpty(gymnasticBarbell.getEndDate())){
        		logger.debug("查询孕妇血糖日期参数单个为空");
            	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参错误,单个日期参数无法查询");
        	}else if(!gymnasticBarbell.getStartDate().equals(gymnasticBarbell.getEndDate()) && DateTimeUtil.compareDate(gymnasticBarbell.getStartDate(),gymnasticBarbell.getEndDate(), DateTimeUtil.yyyy_MM_dd)>-1){
        		logger.debug("查询孕妇血糖日期参数错误:开始日期大于结束日期");
            	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参错误,开始日期大于结束日期");
        	}
        }
        //计算分页参数
        gymnasticBarbell.setStartNum((gymnasticBarbell.getCurPage()-1)*gymnasticBarbell.getPageSize());
        //查询列表数据
        Map<String,Object> rspMap = gymnasticBarbellService.listGymnasticBarbellForPage(gymnasticBarbell);
        logger.debug("out listGymnasticBarbellForPage----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
    /**
     * 根据条件查询
     */
    @ResponseBody
    @RequestMapping("/listGymnasticBarbell")
    public ResultModel<Object> listGymnasticBarbell(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        logger.info("in listGymnasticBarbell----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); // 统一节点处理
        if (null == requestParam) {
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }
        //获取实体参数
        PtGymnasticBarbell gymnasticBarbell = (PtGymnasticBarbell) JsonToEnetityUtil.jsonToObject(requestParam, PtGymnasticBarbell.class);
        //判空
        if(StringUtils.isEmpty(gymnasticBarbell.getUserId())){
        	logger.debug("out UserId----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"患者ID不能为空");
        }
        //日期查询条件判断
        if(!StringUtils.isEmpty(gymnasticBarbell.getStartDate())){
        	if(StringUtils.isEmpty(gymnasticBarbell.getEndDate())){
        		logger.debug("查询孕妇血糖日期参数单个为空");
            	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参错误,单个日期参数无法查询");
        	}else if(!gymnasticBarbell.getStartDate().equals(gymnasticBarbell.getEndDate()) && DateTimeUtil.compareDate(gymnasticBarbell.getStartDate(),gymnasticBarbell.getEndDate(), DateTimeUtil.yyyy_MM_dd)>-1){
        		logger.debug("查询孕妇血糖日期参数错误:开始日期大于结束日期");
            	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参错误,开始日期大于结束日期");
        	}
        }
        //查询列表数据
        Map<String,Object> rspMap = gymnasticBarbellService.listGymnasticBarbell(gymnasticBarbell);
        logger.debug("out listGymnasticBarbell----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value="/delGymnasticBarbellById")
    public ResultModel<Object> delGymnasticBarbellById(HttpServletRequest request, String id) throws Exception {
        logger.info("in delGymnasticBarbellById");
        Map<String,Object> rspMap = Collections.synchronizedMap(new HashMap<String,Object>());
        //日期查询条件判断
        if(StringUtils.isEmpty(id)){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参错误,参数id不能为空");
        }
        rspMap = gymnasticBarbellService.delGymnasticBarbellById(id);
        logger.debug("out delGymnasticBarbellById----arg=="+JSON.toJSONString(rspMap));
        return new ResultModel<Object>(rspMap);
    }
}
